package com.nowcoder.community.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author dgx19
 */
public class CommunityUtil {
    /**
    *生成随机字符串，作为激活码
    */
    public static String generateUUID(){
        return UUID.randomUUID().toString().replaceAll("-","");
    }

   /**
   *采用md5加密  只能加密不能解密
   *每个字符串的加密结果是同样的  为了保证安全在密码字符串后加一个随机字符串
    */
    public static String md5(String key){
        if (StringUtils.isBlank(key)){
            //传入key是否为空
            return null;
        }
        return DigestUtils.md5DigestAsHex(key.getBytes());
        //将key转为md5存储
    }
    /**
     *获取json
     *json信息构造方法
     */
    public static String getJSONString(int code, String msg, Map<String,Object> map){

        JSONObject json = new JSONObject();
        json.put("code",code);
        json.put("msg",msg);
        if (map != null){
            for (String key : map.keySet()){
                json.put(key,map.get(key));
            }
        }
        return json.toJSONString();
    }

    public static String getJSONString(int code,String msg){
        return getJSONString(code,msg,null);
    }

    public static String getJSONString(int code){
        return getJSONString(code,null,null);
    }

 /*   public static void main(String[] args) {
        Map<String,Object> map = new HashMap<>();
        map.put("name","张三");
        map.put("age",23);
        System.out.println(getJSONString(0,"ok",map));
    }*/

    public static String replaceHtml(String str) {
        if(str==null||str.trim().equals("")){
            str="";
        }

        str = str.replaceAll("&","&");
        str = str.replaceAll("\n","<br>");
        str = str.replaceAll(" "," ");
        str = str.replaceAll("'","'");
        return str;
    }


}
